package com.solomon.travel.common.web.controller;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.solomon.travel.common.base.controller.BaseController;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Route;
import com.solomon.travel.common.service.AgencyService;
import com.solomon.travel.common.util.ConstantUtils;
import com.solomon.travel.common.util.IdUtils;
import com.solomon.travel.common.util.PageUtils;

@RestController
@RequestMapping("/web/agency")
public class AgencyController extends BaseController{
	
	@Resource
	private AgencyService agencyService;
	
	@RequestMapping("/publish_route")
	public ApiResponse publishRoute(Route route) {
		if (StringUtils.isEmpty(route.getObjectId())) {
			route.setObjectId(IdUtils.uuid());
			route.setCreatedAt(new Date());
			route.setUpdatedAt(route.getCreatedAt());
			route.setAgencyId(currentUser().getObjectId());
			agencyService.add(route);
		}else{
			route.setUpdatedAt(new Date());
			agencyService.update(route);
		}
		return defaultResponse();
	}
	
	@RequestMapping("/list_routes")
	public ApiResponse listRoutes(Page page) {
		Integer route_counts = agencyService.countRoutes(currentUser().getObjectId());
		page = PageUtils.createPage(page, route_counts);
		HashMap<String, Object> result = new HashMap<>();
		result.put("routes", agencyService.listAgencyRoutes(currentUser().getObjectId(), page));
		result.put("page", page);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, result);
	}
	
	@RequestMapping("/route_detail")
	public ApiResponse routeDetail(String routeId) {
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, agencyService.getById(routeId));
	}
	
	@RequestMapping("/route_delete")
	public ApiResponse deleteRoute(String routeId){
		agencyService.delete(routeId);
		return defaultResponse();
	}
}
